#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main()
{
    try
    {
        boost::asio::io_context io;
        tcp::socket socket(io);
        while(true){
            socket.async_connect(tcp::endpoint(boost::asio::ip::make_address("127.0.0.1"), 12345),
                                [&](boost::system::error_code ec)
                                {
                                    if (!ec)
                                    {
                                        std::string msg;
                                        std::cout<<">>";
                                        std::cin>>msg;
                                        boost::asio::async_write(socket, boost::asio::buffer(msg),
                                                                [&](boost::system::error_code ec, std::size_t /*length*/)
                                                                {
                                                                    if (!ec)
                                                                    {
                                                                        char reply[1024];
                                                                        socket.async_read_some(boost::asio::buffer(reply),
                                                                                                [&](boost::system::error_code ec, std::size_t len)
                                                                                                {
                                                                                                    if (!ec)
                                                                                                    {
                                                                                                        std::cout << "Server replied: " << std::string(reply, len) << "\n";
                                                                                                    }
                                                                                                });
                                                                    }
                                                                });
                                    }
                                });

            io.run();
        }
        
    }
    catch (std::exception &e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}
